package com.smartcanteen.user.controller.user;

import com.smartcanteen.result.Result;
import com.smartcanteen.user.domain.dto.CartDTO;
import com.smartcanteen.user.domain.entity.Cart;
import com.smartcanteen.user.mapper.CartMapper;
import com.smartcanteen.user.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/cart")
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private CartMapper cartMapper;

    @PostMapping("/add")
    public Result add(@RequestBody CartDTO cartDTO){
        log.info("将如下菜品/套餐添加进购物车：{}", cartDTO);
        cartService.add(cartDTO);
        return Result.success();
    }

    @PutMapping("/sub")
    public Result sub(@RequestBody CartDTO cartDTO){
        log.info("将如下菜品/套餐数量减一：{}", cartDTO);
        cartService.sub(cartDTO);
        return Result.success();
    }

    @GetMapping("/list/{id}")
    public Result<List<Cart>> getList(@PathVariable Integer id){
        log.info("拿到当前用户的购物车列表");
        List<Cart> cartList = cartService.getList(id);
        return Result.success(cartList);
    }

    @DeleteMapping("/clean/{id}")
    public Result cleanCart(@PathVariable Integer id){
        log.info("清空购物车");
        cartService.clean(id);
        return Result.success();
    }
    @PostMapping("/api/list")
    public List<Cart> list(@RequestBody Cart cart){
        return cartMapper.list(cart);
    }

    /**
     * 清空购物车，仅供接口调用使用
     * @param id
     */
    @DeleteMapping("/api/clean")
    public void cleanCartForApi(@RequestParam Integer id){
        log.info("清空购物车");
        cartMapper.delete(id);
    }

    @PostMapping("/api/insertBatch")
    public void insertBatch(List<Cart> cartList){
        log.info("将如下菜品/套餐添加进购物车：{}", cartList);
        cartMapper.insertBatch(cartList);
    }
}
